import { login, logout, getInfo } from '@/api/user'
import { adminResetPassword } from '@/api/admin'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
const state = {
  token: getToken(),
  id: '',
  mail: '',
  mobile: '',
  name: '',
  password: '',
  username: '',
  manager: false,
  role: ''
}
const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_ID: (state, id) => {
    state.id = id
  },
  SET_MAIL: (state, mail) => {
    state.mail = mail
  },
  SET_MOBILE: (state, mobile) => {
    state.mobile = mobile
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USERNAME: (state, username) => {
    state.username = username
  },
  SET_MANAGER: (state, manager) => {
    state.manager = manager
  },
  SET_PASSWORD: (state, password) => {
    state.password = password
  },
  SET_ROLE: (state, role) => {
    state.role = role
  }
}

const actions = {
  // user login
  consoleLogin({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        if (response.code === '200' || response.code === 200) {
          commit('SET_TOKEN', response.body.token)
          commit('SET_ID', response.body.id)
          commit('SET_USERNAME', response.body.username)
          commit('SET_NAME', response.body.name)
          commit('SET_MANAGER', response.body.manager)
          commit('SET_ROLE', response.body.manager ? 'super' : 'admin')
          setToken(response.body.token)
          resolve()
        } else {
          reject(response.message)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  // reset password
  resetPassword({ commit }, resetPassword) {
    const { oldPassword, password } = resetPassword
    return new Promise((resolve, reject) => {
      adminResetPassword({ oldPassword: oldPassword, password: password }).then(response => {
        if (response.code === '200' || response.code === 200) {
          resolve()
        } else {
          reject(response.message)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        commit('SET_ID', response.body.id)
        commit('SET_USERNAME', response.body.username)
        commit('SET_NAME', response.body.name)
        commit('SET_MAIL', response.body.mail)
        commit('SET_MOBILE', response.body.mobile)
        commit('SET_MANAGER', response.body.manager)
        commit('SET_PASSWORD', response.body.password)
        resolve(response.body)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_ID', '')
        commit('SET_USERNAME', '')
        commit('SET_NAME', '')
        commit('SET_MAIL', '')
        commit('SET_MOBILE', '')
        commit('SET_PASSWORD', '')
        removeToken()
        resetRouter()
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

